package com.mxnet.chongjin.supoman.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.bean.RepairRecord;
import com.mxnet.chongjin.supoman.utils.TimeUtils;

import java.util.List;

/**
 * author:chongjin
 * email:lcj270018655@163.com
 * date:2018/4/24
 * other:
 */

public class LvRepairDetailAdapter extends BaseAdapter {
    private Context mContext;
    public List<RepairRecord.MaintenanceRecordsBean> mList;

    public LvRepairDetailAdapter(Context context, List<RepairRecord.MaintenanceRecordsBean> list) {
        super();
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_repair_detail, parent, false);
            holder = new ViewHolder();
            holder.ivIsdone = convertView.findViewById(R.id.iv_item_repair_detail_isdone);
            holder.tvYmd = convertView.findViewById(R.id.tv_item_repair_detail_ymd);
            holder.tvHms = convertView.findViewById(R.id.tv_item_repair_detail_hms);
            holder.tvState = convertView.findViewById(R.id.tv_item_repair_detail_state);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position == 0) {
            holder.ivIsdone.setVisibility(View.VISIBLE);
            holder.tvState.setTextColor(mContext.getResources().getColor(R.color.regist_agreement));
            holder.tvYmd.setTextColor(mContext.getResources().getColor(R.color.regist_agreement));
            holder.tvHms.setTextColor(mContext.getResources().getColor(R.color.regist_agreement));
        } else {
            holder.ivIsdone.setVisibility(View.INVISIBLE);
            holder.tvState.setTextColor(mContext.getResources().getColor(R.color.near_machine));
            holder.tvYmd.setTextColor(mContext.getResources().getColor(R.color.near_machine));
            holder.tvHms.setTextColor(mContext.getResources().getColor(R.color.near_machine));
        }
        setProgress(mContext, holder.tvState, (mList.get(mList.size() - position - 1).getMaintenanceMemo()));
        holder.tvYmd.setText(TimeUtils.stampToDate(mList.get(mList.size() - position - 1).getCreateTime(), "yyyy-MM-dd"));
        holder.tvHms.setText(TimeUtils.stampToDate(mList.get(mList.size() - position - 1).getCreateTime(), "HH:mm:ss"));

        return convertView;
    }

    public static class ViewHolder {
        ImageView ivIsdone;
        TextView tvYmd, tvHms, tvState;
    }

    public static void setProgress(Context context, TextView textView, String state) {
        switch (state) {
            case "申请维修，等待受理":
                textView.setText(context.getResources().getString(R.string.order_state_1));
                break;
            case "维修已受理，等待上门":
                textView.setText(context.getResources().getString(R.string.order_state_2));
                break;
            case "维修已完成":
                textView.setText(context.getResources().getString(R.string.order_state_3));
                break;
        }
    }
}
